﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using KPIS.GERP.Models.ICM;

namespace KPIS.GERP.FileImportToDb.Models
{
    public class VM1
    {
        public beneficiary Beneficiary { get; set; }
        public MasContact MasContact { get; set; }
        public MasContactBank MasContactBank { get; set; }

        public bool IsFullNameEqual
        {
            get
            {
                return MasContact != null;
            }
        }

        public bool IsBankSame
        {
            get
            {
                if (MasContact != null && MasContactBank != null &&
                    Convert.ToInt32(MasContactBank.MasBankBranch.Code) == Convert.ToInt32(Beneficiary.branch_code) &&
                    Convert.ToInt32(MasContactBank.MasBankBranch.Bank.Code) == Convert.ToInt32(Beneficiary.bank_Code))
                {
                    return true;
                }

                return false;
            }
        }

        public bool FindContactInCurrentDb(List<MasContact> contacts )
        { 
            var excelFirstName = FirstName(Beneficiary.fullname);
            var excelLastName = LastName(Beneficiary.fullname);

            foreach (var cont in contacts)
            {
                var contFirstName = FirstName(cont.NameTh);
                var contLastName = LastName(cont.NameTh);
                if ((!string.IsNullOrWhiteSpace(excelFirstName) && !string.IsNullOrWhiteSpace(contFirstName) && excelFirstName == contFirstName) &&
                    (!string.IsNullOrWhiteSpace(excelLastName) && !string.IsNullOrWhiteSpace(contLastName) && excelLastName == contLastName))
                {
                    MasContact = cont;
                    return true;
                }
            }
            return false; 
        }

        public bool FindContactBankInCurrentDb(List<MasContactBank> contactBanks)
        {
            if (MasContact != null)
            {
                MasContactBank = contactBanks.FirstOrDefault(o => o.ConId == MasContact.ConId);
                return true;
            }
            return false;
        }
        private string FirstName(string str)
        {
            str = str.Replace("นพ.", "")
                    .Replace("นาย", "")
                    .Replace("นาง", "")
                    .Replace("น.ส.", "")
                    .Replace("น.ต.", "")
                    .Replace("เภสัชกร", "")
                    .Replace("พล ร.ต.", "")
                    .Replace("พ.ต.", "")
                    .Replace("ดร.", "")
                    .Replace("ภก.", "")
                    .Replace("รศ.พญ.", "")
                    .Replace("พญ.", "")
                    .Replace("น.ท.หญิง", "")
                    .Replace("อ.", "")
                    .Replace("ผศ.", "")
                    .Replace("อาจารย์", "");
            var lastName = str.Split(' ').Length > 1 ? str.Split(' ')[str.Split(' ').Length - 1] : "";
            str = str.Replace("lastName", "").Trim();
            var firstName = str.Split('.').Length > 0 ? str.Split('.')[str.Split('.').Length - 1] : "";
            firstName = firstName.Split(' ').Length > 0 ? firstName.Split(' ')[0] : "";
            return firstName.ToLower();
        }
        private string LastName(string str)
        {
            str = str.Replace("นพ.", "")
                    .Replace("นาย", "")
                    .Replace("นาง", "")
                    .Replace("น.ส.", "")
                    .Replace("น.ต.", "")
                    .Replace("เภสัชกร", "")
                    .Replace("พล ร.ต.", "")
                    .Replace("พ.ต.", "")
                    .Replace("ดร.", "")
                    .Replace("ภก.", "")
                    .Replace("รศ.พญ.", "")
                    .Replace("พญ.", "")
                    .Replace("น.ท.หญิง", "")
                    .Replace("อ.", "")
                    .Replace("ผศ.", "")
                    .Replace("อาจารย์", "");
            var lastName = str.Split(' ').Length > 1 ? str.Split(' ')[str.Split(' ').Length - 1] : "";
            str = str.Replace("lastName", "").Trim();
            var firstName = str.Split('.').Length > 0 ? str.Split('.')[str.Split('.').Length - 1] : "";
            firstName = firstName.Split(' ').Length > 0 ? firstName.Split(' ')[0] : "";
            return lastName.ToLower();
        }
    }
}